import 'package:flutter/material.dart';
import 'package:yqys_target/gen/assets.gen.dart';
import 'package:yqys_target/utils/yq_colors.dart';

enum MyCharityStatus {
  notPassed('未通过', Colors.red),
  pending('待审核', Colors.blue),
  inProgress('进行中', Colors.green),
  completed('已完成', Colors.grey);

  final String label;
  final Color color;

  const MyCharityStatus(this.label, this.color);
}

enum MyCredentialStatus {
  needUpload('上传凭证', Colors.green),
  checked('已审核', Colors.green),
  waitToChecking('待审核', Colors.green);

  final String label;
  final Color color;

  const MyCredentialStatus(this.label, this.color);

  Color get textColor {
    switch (this) {
      case needUpload:
        return Colors.white;
      case checked:
      case waitToChecking:
        return color;
    }
  }

  Color get backgroundColor {
    switch (this) {
      case needUpload:
        return color;
      case checked:
      case waitToChecking:
        return Colors.white;
    }
  }
}

enum CharityAndCredentialType {
  myCharity,
  myCredential,
}

class CharityAndCredentialListItem extends StatelessWidget {
  final String? imageUrl;
  final String title;
  final CharityAndCredentialType type;
  final dynamic status; // MyCharityStatus or MyCredentialStatus
  final VoidCallback? onTap;

  const CharityAndCredentialListItem({
    super.key,
    this.imageUrl,
    required this.title,
    required this.type,
    required this.status,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _shouldHandleTap ? onTap : null,
      child: Container(
        margin: const EdgeInsets.only(bottom: 16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              offset: const Offset(0, 2),
              blurRadius: 4,
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(16),
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      title,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: YQColors.primary,
                      ),
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 13,
                      vertical: 8,
                    ),
                    decoration: BoxDecoration(
                      color: _getBackgroundColor(),
                      border: Border.all(
                        color: _getBorderColor(),
                      ),
                      borderRadius: BorderRadius.circular(100),
                    ),
                    child: Text(
                      _getStatusLabel(),
                      style: TextStyle(
                        color: _getTextColor(),
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            ClipRRect(
              borderRadius: const BorderRadius.only(
                bottomLeft: Radius.circular(10),
                bottomRight: Radius.circular(10),
              ),
              child: Image.asset(
                imageUrl ?? Assets.images.project1.path,
                width: double.infinity,
                height: 130,
                fit: BoxFit.cover,
              ),
            ),
          ],
        ),
      ),
    );
  }

  bool get _shouldHandleTap {
    if (type == CharityAndCredentialType.myCredential) {
      return status == MyCredentialStatus.needUpload;
    }
    return true;
  }

  String _getStatusLabel() {
    if (type == CharityAndCredentialType.myCharity) {
      return (status as MyCharityStatus).label;
    } else {
      return (status as MyCredentialStatus).label;
    }
  }

  Color _getBorderColor() {
    if (type == CharityAndCredentialType.myCharity) {
      return (status as MyCharityStatus).color;
    } else {
      return Colors.green; // 回传凭证类型边框永远是绿色
    }
  }

  Color _getBackgroundColor() {
    if (type == CharityAndCredentialType.myCharity) {
      return Colors.white; // 我的益事类型背景永远是白色
    } else {
      return (status as MyCredentialStatus).backgroundColor;
    }
  }

  Color _getTextColor() {
    if (type == CharityAndCredentialType.myCharity) {
      return (status as MyCharityStatus).color;
    } else {
      return (status as MyCredentialStatus).textColor;
    }
  }
} 